<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/db.class.php');
    require_once($config['app_dir'] . '/includes/mailer.class.php');
    require_once($config['app_dir'] . '/includes/template.class.php');

    require_once($config['app_dir'] . '/classes/user.data.php');
    //---------------------------------------------------------------------------------------------------
    class objRemindPasswordServer {
	    protected $UserRecord = false;
	    protected $email = false;

	public function __construct($email) {
	    $this->email = $email;
	} //constructor

	public function isValid() {
	    return eregi("^[a-zA-Z0-9]+[_a-zA-Z0-9-]*(\.[_a-z0-9-]+)*@[a-z??????0-9]+(-[a-z??????0-9]+)*(\.[a-z??????0-9-]+)*(\.[a-z]{2,4})$", $this->email);
	} //Valid

	public function Find() {
	    $UserModel = new objUserModel();
        $this->UserRecord = $UserModel->ActiveRecord();
        $this->UserRecord->Load("Login=\"$this->email\"");
	    return $this->UserRecord->login;
	} //Find

    function GeneratePassword($length = 8) {
      	$password = '';
      	//characters allowed
      	$allowed = "0123456789bcdfghjkmnpqrstvwxyz@_";
     	$i = 0;
      	// Loop until password string is the required length
      	while ($i < $length) {
            // Select random character allowed string
            $char = substr($allowed, mt_rand(0, strlen($allowed)-1), 1);
            // Add random character to password string
            $password .= $char;
            $i++;
        }//while
        // Return random password
        return $password;
    }//GeneratePassword

	public function Send() {
        $this->UserRecord->password = $this->GeneratePassword();
        $this->UserRecord->Save();
        //----------------------------------------------------------------------
	    $Mailer = new objMailer();
        $Mailer->isHTML( 1==$this->UserRecord->messagetype );
        $Mailer->AddAddress($this->UserRecord->login, $this->UserRecord->name);

        $Mailer->Subject = 'IntelliForex Credentials';
		$Mailer->From = 'support@intelli4x.com';
        $BodyTemplate = new objTemplate();
        $BodyTemplate->Assign('user', $this->UserRecord);
        $BodyTemplate->Fetch('EmailRemind.tpl');
        $Mailer->Body = $BodyTemplate->Fetch('EmailRemind.tpl');


	    if( !$Mailer->Send() ) {
            throw new Exception("There was an error sending the message. ". $Mailer->ErrorInfo);
        }
	} //Send

	public function After() {
	    $AfterTemplate = new objTemplate();
        $AfterTemplate->Assign('user', $this->UserRecord);
	    return $AfterTemplate->Fetch('users/_after_Remind.tpl');
	} //After

    } //class RemindPasswordServer
    //---------------------------------------------------------------------------------------------------
    function processRemindPasswordForm($aFormValues) {
        $Response = new xajaxResponse();
        $bError = false;
        $RemindPasswordServer = new objRemindPasswordServer($aFormValues['login']);
        //----------------------------------------------------------------------
        if( !$RemindPasswordServer->isValid() )	{
			$InfoTemplate = new objTemplate();



        	//$Response->addAssign("informDiv","innerHTML", "Please enter valid e-mail address that you have used during account registration.");
			$Response->addAssign("informDiv","innerHTML", $InfoTemplate->Fetch('_Remind_validation.tpl'));
        	$bError = 1;
        } elseif( !$RemindPasswordServer->Find() ) {
        	//$Response->addAssign("informDiv","innerHTML", "There  is no IntelliForex account associated with the e-mail specified. Please enter e-mail that you have used during account registration.");
			$Response->addAssign("informDiv","innerHTML", $InfoTemplate->Fetch('_Remind_exist.tpl'));
        	$bError = 2;
        }
//	else $Response->addAssign("informDiv","innerHTML", "Please enter valid e-mail address that you have used during account registration.");
	//----------------------------------------------------------------------
        if (!$bError) {
                try {
                        $RemindPasswordServer->Send();
//                        $Response->addAssign("formDiv", "style.textAlign", "center");
                        $Response->addAssign("formDiv", "innerHTML", $RemindPasswordServer->After());
//        				$Response->addAssign("formWrapper", "style.backgroundColor", "rgb(67,97,149)");
						$Response->addAssign("informDiv", "innerHTML", '');
                } catch(exception $e) {
                        $Response->addAssign("informDiv","innerHTML", 'Error: '.$e->getMessage());
                        $Response->addAssign("submitButton","disabled",false);
                        $Response->addAssign("submitButton","value","try later");
                }
        }
        else {
        	$Response->addAssign("submitButton","value","send");
        	$Response->addAssign("submitButton","disabled",false);
        }
        //----------------------------------------------------------------------
        return $Response;
    } //processRemindPasswordForm
//-------------------------------------------------------------------------------------------------------
?>
